package com.xhu.controller;

import com.xhu.base.ConnectionMySQL;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.update.Update;

import java.io.StringReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 王涛
 * @Created by WT on 2019/3/29.
 */
public class PreProcessTest {

    /**
     * 测试元数据表是否创建，增删改查功能是否正常
     */
    public void testSSDB() {
        try {
            Connection conn = ConnectionMySQL.openConnection();
            ResultSet rs = conn.getMetaData().getTables(null, null, "metadata", null);
            System.out.println("请等待测试完成");
            if (!rs.next()) {
                java.sql.Statement createMetadataTable = conn.createStatement();
                createMetadataTable.execute(
                        "create table metadata(tablename varchar(50),columnname varchar(200),datatype varchar(100),opekey text,homkey text,primary key(tablename,columnname));");
                System.out.println("元数据表创建模块测试完成");
            }
            try {
                CCJSqlParserManager parserManager = new CCJSqlParserManager();
                String inputSQL = "create table test(id int);";
                Statement statement = parserManager.parse(new StringReader(inputSQL));
                CreateTable createTable = (CreateTable) statement;
                CreateTableDeparser.handler(createTable, conn);
                System.out.println("创建表模块测试完成");

                inputSQL = "insert into test(id) values(1);";
                statement = parserManager.parse(new StringReader(inputSQL));
                Insert insert = (Insert) statement;
                InsertDeparser.handler(insert, conn);
                System.out.println("插入表模块测试完成");

                inputSQL = "select id from test where id = 0;";
                statement = parserManager.parse(new StringReader(inputSQL));
                Select select = (Select) statement;
                SelectSQL.handler(select, conn);
                System.out.println("查询表模块测试完成");

                inputSQL = "update test set id = 2 where id =1";
                statement = parserManager.parse(new StringReader(inputSQL));
                Update update = (Update) statement;
                UpdateDeparser.handler(update, conn);
                System.out.println("更新表模块测试完成");

                inputSQL = "delete from test where id > 0;";
                statement = parserManager.parse(new StringReader(inputSQL));
                Delete delete = (Delete) statement;
                DeleteDeparser.handler(delete, conn);
                System.out.println("删除表模块测试完成");

                java.sql.Statement smt = conn.createStatement();
                smt.execute("drop table test;");
                smt.execute("delete from metadata where tablename = 'test';");

                System.out.println("全部测试完成");
                smt.close();
                Client.tableNameList.clear();
                Client.encColumnNameList.clear();
            } catch (JSQLParserException e) {
                e.printStackTrace();
            }
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
